package gameam.ui;

import gameam.business.*;
import gameam.data.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;


public class SimpleUI
{
	private static final Logger LOGGER = Logger.getLogger(SimpleUI.class.getName());
	private static MCBaseTest _test;
	
	private static void LoadTest()
	{
		//load from database
		//for now make fake test
		// _test = new MCBaseTest();
		
		// MCBaseQuestion q1 = new MCBaseQuestion("What body part is on the front of your head?");
		// q1.AddAnswer(new MCAnswer(1,"Your Face..."));
		// q1.AddAnswer(new MCAnswer(0,"The back of your FACE!"));
		
		// MCBaseQuestion q2 = new MCBaseQuestion("Who is a cunt-FACE?");
		// q2.AddAnswer(new MCAnswer(0,"Me"));
		// q2.AddAnswer(new MCAnswer(1,"Who?"));
		// q2.AddAnswer(new MCAnswer(2,"The lecturer."));
		
		// _test.AddQuestion(q1);
		// _test.AddQuestion(q2);
		//TestDataLoader db = new TestDataLoader();
		DatabaseLoader db = new DatabaseLoader("testDB.mdb");
		_test = db.getTest();
	}
	
	private static int ReadAnswer(BufferedReader input)
	{
		String ans = "";
		
		try
		{
			ans = input.readLine();
			int ians = Integer.parseInt(ans);
			return ians;
		}
		catch (Exception e)
		{
			//log
			return -1;
		}
	}
	
	private static void PrintCurrentQuestion()
	{
		MCBaseQuestion q = _test.GetCurrentQuestion();
		String temp = "";
		
		System.out.println(q.GetText());
		for (int i = 0; i < q.GetAnswerCount(); i++)
		{
			temp = Integer.toString(i) + ") ";
			temp = temp + q.GetAnswerAtIndex(i).GetText();
			System.out.println(temp);
		}
	}
	
	public static void main(String[] args)
	{
		try
		{
			MCLogger.Setup();
		}
		catch (IOException e)
		{
			e.printStackTrace();
			throw new RuntimeException("Could not create log file!");
		}
		
		LOGGER.setLevel(Level.INFO);
		LOGGER.info("Simple UI Started.");
	
		System.out.println("G.A.M.E Multiple Choice Testing System");
		
		BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
		
		System.out.println("Please type the number that corresponds to the correct answer and press ENTER...");
		System.out.println("");
		
		boolean testOver = false;
		int answer = -1;
		int valid = -1;
		
		LoadTest();
		
		_test.ResetTest();
		LOGGER.info("Test started.");
		
		while(testOver == false)
		{			
			PrintCurrentQuestion();
			System.out.println("Your answer:");
			valid = -1;
			while (valid == -1)
			{
				answer = ReadAnswer(input);
				valid = _test.GetCurrentQuestion().ToggleAnswer(answer);
				
				if (valid == -1)
					System.out.println("Please type a valid answer you fool:");
			}
			System.out.println("");
			
			int res = _test.ProceedToNextQuestion();
			if (res != -1)
			{
				testOver = true;
				System.out.println("Well done you finished the test!");
				
				float percent = (float)res / (float)_test.MaxMarkForTest();
				percent *= 100.0f;
				
				String mark = "Your mark is " + Integer.toString((int)percent) + "%...";
				LOGGER.info("Test finished. " + mark);
				System.out.println(mark);
			}
		}
	}
}